package com.lovely602.langchain4j.param.config;


import com.lovely602.langchain4j.param.listener.QWenChatModelListener;
import com.lovely602.langchain4j.param.service.ChatAssistant;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.List;

/**
 *
 * @author lizhixing
 */
@Configuration
public class LLMConfig {

    /**
     *
     * 注册通义千问聊天模型
     */
    @Bean("chatModelQwen")
    public ChatModel chatModelQwen() {
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("AI_DASHSCOPE_API_KEY"))
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .modelName("qwen-plus")
                .logRequests(true) // 打印请求日志，日志级别设置为info生效
                .logResponses(true) // 打印响应日志，日志级别设置为info生效
                .listeners(List.of(new QWenChatModelListener())) // 添加监听器
                .maxRetries(2)
                .timeout(Duration.ofSeconds(30))
                .build();
    }

    /**
     * 两种均可
     * 注册deepSeek聊天模型
     */
    @Bean("chatModelDeepSeek")
    public ChatModel chatModelDeepSeek() {
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("AI_DEEPSEEK_API_KEY"))
                .baseUrl("https://api.deepseek.com/v1")
                .modelName("deepseek-chat")
                .build();
    }

    /**
     * High-Api https://docs.langchain4j.dev/tutorials/ai-services#simplest-ai-service
     */
    @Bean
    public ChatAssistant chatAssistant(@Qualifier("chatModelQwen") ChatModel chatModel) {
        return AiServices.create(ChatAssistant.class, chatModel);
    }

}
